package com.example.mydatasingleservice.services;

import android.util.Log;

import androidx.lifecycle.Observer;

import com.example.mydatasingleservice.DataSingle;
import com.example.mydatasingleservice.bean.AData;
import com.example.mydatasingleservice.bean.BData;
import com.example.mydatasingleservice.bean.CData;

public class CService extends BaseServiceListenner{
    private final static String TAG = "CService";

    @Override
    protected void initLiveDataObserver() {
        super.initLiveDataObserver();
        final Observer<CData> cDataObserver = new Observer<CData>() {
            @Override
            public void onChanged(CData cData) {
                switch (cData.getcDataState()){
                    case NO:
                        Log.d(TAG, "C 任务结束！");
                        break;
                    case INIT:
                        Log.d(TAG, "INIT: ");
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, "INIT: 睡眠");
                                try {
                                    Thread.sleep(1000);
                                    Log.d(TAG, "INIT: 睡眠结束");
                                    DataSingle.getInstance().changeCdata(CData.CDataState.RUNNING);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                        break;
                    case RUNNING:
                        Log.d(TAG, "RUNNING: ");
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, "RUNNING: 睡眠");
                                try {
                                    Thread.sleep(2000);
                                    Log.d(TAG, "RUNNING: 睡眠结束");
                                    DataSingle.getInstance().changeCdata(CData.CDataState.FINISH);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                        break;
                    case FINISH:
                        Log.d(TAG, "FINISH: ");
                        DataSingle.getInstance().changeBdata(BData.BDataState.RUNNING);
                        DataSingle.getInstance().changeCdata(CData.CDataState.NO);
                        break;
                }
            }
        };
        DataSingle.getInstance().getcDataMutableLiveData().observe(this,cDataObserver);
    }


}
